package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /*
     *
     *
     * */
    //员工登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //1 加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2根据用户名查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //3判断
        if (emp == null) {
            return R.error("登陆失败");
        }

        //4密码比对
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //5查看员工状态
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //6登录成功,返回结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);


    }

    //退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前员工登录ID
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    //新增员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpServletRequest request) {
        log.info("新增员工，员工信息:{}", employee.toString());
        //设置初始密码，并且进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
       // employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
      //  Long empId = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(empId);
        //employee.setUpdateUser(empId);
        System.out.println("第一");
        employeeService.save(employee);
        System.out.println("第三");
        R.success("新增员工成功");
        return R.success("新增成功");
    }

    @GetMapping("/page")
    public R<Page> list(int page, int pageSize, String name) {
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        //构造分页构造器
        Page page1 = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.hasText(name), Employee::getName, name);
        //排序条件
        queryWrapper.orderByAsc(Employee::getUpdateTime);

        employeeService.page(page1, queryWrapper);
        return R.success(page1);


    }
@PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){

      //  Long empId = (Long) request.getSession().getAttribute("employee");
       // employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
@GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable Long id){

       Employee employee = employeeService.getById(id);
        return R.success(employee);

    }



}
